﻿using Microsoft.EntityFrameworkCore;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.UserManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Sys
{
    /// <summary>
    /// 用户操作类
    /// </summary>
    public class Sys_LoginHandle
    {
        private readonly SJDBContext _context;

        public Sys_LoginHandle(SJDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<SysLogin> Login(string loginName, string password)
        {
            try
            {
                IQueryable<SysLogin> query = _context.SysLogins;

                // 如果提供了查询条件，则应用它
                if (!string.IsNullOrWhiteSpace(loginName) && !string.IsNullOrWhiteSpace(password))
                {
                    query = query.Where(u => u.LoginName == loginName && u.Password == password && u.IsUsed == 1);
                    // 执行查询并返回结果
                    return await query.FirstAsync();
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Sys_LoginHandle=>Login", ex);
            }
            return null;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(SysLogin entity)
        {
            try
            {
                _context.SysLogins.Add(entity);
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Sys_LoginHandle=>Create", ex);
                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(SysLogin entity)
        {
            try
            {
                _context.Attach(entity);
                PropertyInfo[] propertyInfos = typeof(SysLogin).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (var property in propertyInfos)
                {
                    if (property.Name == "LoginName" || property.Name == "IsUsed")
                    {
                        _context.Entry(entity).Property(property.Name).IsModified = true;
                    }
                }
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Sys_LoginHandle=>Update", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteById(string id)
        {
            try
            {
                var entity = _context.SysLogins.Find(id);
                if (entity != null)
                {
                    _context.SysLogins.Remove(entity);
                    return _context.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Sys_LoginHandle=>DeleteById", ex);
            }
            return false;
        }
    }
}